import { _decorator, Sprite, Label, Texture2D, SpriteFrame, ImageAsset, EventTouch, Node } from 'cc';
import {smc} from "db://assets/script/game/common/singletonModuleComp";
import {CCComp} from "db://oops-framework/module/common/CCComp";
import {Player} from "db://assets/script/game/player/player";
import {ecs} from "db://oops-framework/libs/ecs/ECS";
import {oops} from "db://oops-framework/core/Oops";
import {UIID} from "db://assets/script/game/common/config/gameUiConfig";
import {RudyDeskPanel} from "./rudyDeskPanel";
import {GameType} from "db://assets/script/game/constants/common";

const {ccclass, property} = _decorator;

@ccclass('DatingViewComp')
@ecs.register("DatingViewComp")
export class DatingViewComp extends CCComp {
    @property({
        type: Sprite,
        tooltip: "头像"
    })
    avatar: Sprite = null!

    @property({
        type: Label,
        tooltip: "昵称"
    })
    nickname: Label = null!

    @property({
        type: Label,
        tooltip: "玩家 shortId"
    })
    shortId: Label = null!

    @property({
        type: Label,
        tooltip: "金豆数"
    })
    ruby: Label = null!

    @property({
        type: Label,
        tooltip: "钻石数"
    })
    gem: Label = null!

    @property({
        type: Node,
        tooltip: "禁止点击"
    })
    blocker: Node = null!


    onLoad() {
        smc.dating.add(this)
        // 检查是否有未完成的房间
        smc.dating.restoreDesk(false)
        // 默认可以点击
        this.blocker.active = false
    }

    start() {
        // 更新昵称, 头像等等
        const player = Player.getInstance()
        this.updateGem(player.model.gem)
        this.updateRuby(player.model.ruby)
        oops.res.loadRemote(player.model.avatar, this.loadAvatar.bind(this))
        this.shortId.string = player.model.shortId.toString()
        this.nickname.string = player.model.nickname
    }

    enableInput() {
        this.blocker.active = false
    }

    disableInput() {
        this.blocker.active = true
    }
    reset(): void {
    }

    // 更新金豆
    updateRuby(value: number) {
        this.ruby.string = value.toString()
    }

    // 钻石
    updateGem(value: number) {
        this.gem.string = value.toString()
    }

    loadAvatar(err: Error, data: ImageAsset) {
        if (err != null) {
            // 下载失败
            console.error(err)
            return;
        }
        const texture = new Texture2D();
        texture.image = data;
        const spriteFrame = new SpriteFrame();
        spriteFrame.texture = texture;
        this.avatar.spriteFrame = spriteFrame;
    }

    // 选择场次
    clickSelectRubyDesk(e: EventTouch, data: string) {
        if (data !== GameType.mj) {
            console.error("only mj supported, but receive", data)
        }
        const onAdded = (node: Node, gameType: any) => {
            const view = node.getComponent(RudyDeskPanel)
            view.setOnEnter(gameType)
        }
        oops.gui.open(UIID.selectRubyDesk, data, { onAdded })
    }

    // 打开数字输入法
    clickOpenDeskNoKeyboard() {
        oops.gui.open(UIID.deskNoKeyboard)
    }
}


